package com.videoweb.controller;

import com.videoweb.entity.Menu;
import com.videoweb.entity.User;
import com.videoweb.model.LoginBody;
import com.videoweb.model.LoginUser;
import com.videoweb.service.IMenuService;
import com.videoweb.service.IUserService;
import com.videoweb.service.impl.PermissionService;
import com.videoweb.service.impl.SysLoginService;
import com.videoweb.utils.AjaxResult;
import com.videoweb.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;


/**
 * 登录验证
 * 
 * @author lmr
 */
@RestController
public class LoginController
{

    @Autowired
    SysLoginService loginService;

    @Autowired
    IUserService userService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private PermissionService permissionService;
//登录进去的时候给权限，给菜单

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult result = AjaxResult.success();
        // 生成令牌
//        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword());

//        给token
        result.put("token", token);

        return result;
    }
    //    写注册接口的时候记得在user_role中间表中添加角色
//    因为查询用户角色的时候会关联到角色表，不写查询不到
//    还要分配收藏夹（默认的）
    @PostMapping("/register")
    public AjaxResult register(@RequestBody User user)
    {
        AjaxResult result;
        if (userService.addUser(user)) {
            result = AjaxResult.success("注册成功");
        }else {
            result = AjaxResult.error("注册失败");
        }
//        System.out.println(ajax);
//        Result result =new Result<>();
        return result;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {


        User user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }








    @GetMapping("/getRouters")
//
    public AjaxResult getRouters()
    {
//        Long userId = 1L;
        Long userId = SecurityUtils.getUserId();
//        在这里得到的是菜单为（M,C的集合）  并且对菜单进行构建父子树
        List<Menu> menus = menuService.selectMenuTreeByUserId(userId);
//
        return AjaxResult.success(menuService.buildMenus(menus));
    }




}
